<template>
    <div>
        <!-- 搜索组件 -->
        <search-form :items="formItems" :params="params" :on-submit="onSearch" :btn-loading="loading" />

        <!-- 表格区域 -->
        <div class="table-body">
            <!-- 左上角按钮 -->
            <div class="table-right-top">
            </div>

            <!-- 表格 -->
            <el-table border style="width: 100%;" :data="data" v-loading="loading">
                <el-table-column align="center" prop="order_no" :label="$i18n.t('ORDER_NO')" fixed="left" :min-width="170"></el-table-column>
                <el-table-column align="center" prop="real_name" :label="$i18n.t('REAL_NAME')" :min-width="150"></el-table-column>
                <el-table-column align="center" prop="mobile" :label="$i18n.t('PHONE_NUMBER')" :min-width="150"></el-table-column>
                <el-table-column align="center" prop="apply_amount" :label="$i18n.t('APPLY_AMOUNT')" :min-width="100"></el-table-column>
                <el-table-column align="center" prop="apply_term" :label="$i18n.t('APPLY_TERM')" :min-width="100"></el-table-column>
                <el-table-column align="center" prop="loan_number" :label="$i18n.t('LOAN_NUMBER')" :min-width="100">
                    <template slot-scope="scope">
                        <el-tag size="mini" :type="scope.row.loan_number > 1 ? 'success' : 'danger'" effect="dark">
                            {{ scope.row.loan_number }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="create_time" :label="$i18n.t('CREATE_TIME')" :min-width="150"></el-table-column>
                <el-table-column align="center" prop="assign_time" :label="$i18n.t('ASSIGN_TIME')" :min-width="150"></el-table-column>
                <el-table-column align="center" prop="status" :label="$i18n.t('FOLLOW_STATUS')" fixed="right" :min-width="150">
                    <template slot-scope="scope">
                        <el-tag size="mini" v-if="scope.row.status === 0" type="success" effect="dark">{{ scope.row.status_text }}</el-tag>
                        <el-tag size="mini" v-else-if="scope.row.status === 1" type="danger" effect="dark">{{ scope.row.status_text }}</el-tag>
                        <el-tag size="mini" v-else-if="scope.row.status === 2" type="warning" effect="dark">{{ scope.row.status_text }}</el-tag>
                        <el-tag size="mini" v-else type="info" effect="dark">{{ scope.row.status_text }}</el-tag>
                    </template>
                </el-table-column>
                <!-- 右侧按钮 -->
                <el-table-column align="center" :label="$i18n.t('ACTION')" fixed="right" :width="150">
                    <template slot-scope="scope">
                        <el-link v-auth="'handle'" icon="el-icon-view" @click="onView(scope.row)">{{ $i18n.t('HANDLE') }}</el-link>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <div class="table-pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next, ->, sizes"
                    :page-size.sync="page.perPage"
                    :total="page.total"
                    :page-count="page.pageCount"
                    :current-page.sync="page.currentPage"
                    :page-sizes="PAGES_SIZE"
                    @current-change="onCurrentPageChange"
                    @size-change="onPerPageChange"
                >
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
    import SearchForm from '@/components/SearchForm'
    import { getApproveTaskList } from '@/api/approve'
    import { PAGES_SIZE } from '@/config/constants'

    export default {
        name: 'ApproveTaskList',
        components: { SearchForm },
        data () {
            return {
                PAGES_SIZE,
                params: {
                },
                data: [],
                page: {
                    total: 0,
                    currentPage: 1,
                    perPage: PAGES_SIZE[0],
                    pageCount: 1
                },
                loading: false
            }
        },
        created () {
            this.fetch()
        },
        computed: {
            formItems () {
                const LoanType = this.$store.getters.options.LoanedTypeList
                // 构建搜索表单
                return [
                    { title: this.$i18n.t('ORDER_NO'), type: 'input', key: 'order_no', maxlength: 50 },
                    { title: this.$i18n.t('PHONE_NUMBER'), type: 'input', key: 'mobile', maxlength: 20 },
                    { title: this.$i18n.t('REAL_NAME'), type: 'input', key: 'real_name', maxlength: 20 },
                    { title: this.$i18n.t('CUSTOMER_CHANNEL'), type: 'select', key: 'channel_id', options: this.$store.getters.options.ChannelList },
                    { title: this.$i18n.t('PRODUCT_NAME'), type: 'select', key: 'product_id', options: this.$store.getters.options.ProductList },
                    { title: this.$i18n.t('FOLLOW_STATUS'), type: 'select', key: 'status', options: this.$store.getters.options.ApproveTaskStatusList.filter(item => item.value >= 3) },
                    { title: this.$i18n.t('CREATE_TIME'), type: 'date-range', key: 'create_time' },
                    { title: this.$i18n.t('ASSIGN_TIME'), type: 'date-range', key: 'assign_time' },
                    { title: this.$i18n.t('LOAN_TYPE'), type: 'select', key: 'loan_type', options: LoanType }
                ]
            }
        },
        methods: {
            // 搜索按钮
            onSearch () {
                this.fetch()
            },
            // 从接口拉取数据
            fetch () {
                this.loading = true
                getApproveTaskList({ ...this.params, page: this.page.currentPage, perPage: this.page.perPage }).then((res) => {
                    // eslint-disable-next-line camelcase
                    const { data, last_page, total } = res.result
                    this.data = data
                    this.page.total = total
                    // eslint-disable-next-line camelcase
                    this.page.pageCount = last_page
                }).finally(() => {
                    this.loading = false
                })
            },
            // 页数切换
            onCurrentPageChange (page) {
                this.page.currentPage = page
                this.fetch()
            },
            // 每页显示数量修改
            onPerPageChange (perPage) {
                this.page.perPage = perPage
                this.fetch()
            },
            onView (row) {
                this.$router.push({
                    path: '/approve/detail',
                    query: {
                        id: row.id,
                        type: 'handle'
                    }
                })
            }
        }
    }
</script>

<style lang="less" scoped>
</style>
